﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF REMOTE PAGE PROVIDER FOR EPISERVER CMS.
REMOTE PAGE PROVIDER FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

REMOTE PAGE PROVIDER FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH REMOTE PAGE PROVIDER FOR EPISERVER CMS]. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Web;
using System.ServiceModel;
using EPiServer.Configuration;
using EPiServer.Core;
using log4net;
using EPiServer.WCF;

namespace EPiServer.Labs.RemoteProvider
{
    class RemoteProviderModule : IHttpModule
    {
        private static ServiceHost _RemoteProviderServiceHost;
        private static readonly ILog _log = LogManager.GetLogger(typeof(RemoteProviderModule));
        private static object _lock = new object();
        private static int refs = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="RemoteProviderModule"/> class.
        /// </summary>
        public RemoteProviderModule()
        {
        }

        #region IHttpModule Members

        /// <summary>
        /// Initializes an <see cref="RemoteProviderModule"/> module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
            lock (_lock)
            {
                if (_RemoteProviderServiceHost == null)
                {
                    try
                    {
                        _RemoteProviderServiceHost = new GenericWCFServiceHost<RemoteProviderServer>();
                        _RemoteProviderServiceHost.Open();
                    }
                    catch (Exception e)
                    {
                        _RemoteProviderServiceHost = null;

                        string message = "Error creating/opening service host for RemoteProviderServer class";
                        _log.Error(message, e);
                        throw new EPiServerException(message, e);
                    }
                }
                refs++;
            }
        }

        /// <summary>
        /// Disposes of the resources (other than memory) used by this <see cref="RemoteProviderModule"/> instance.
        /// </summary>
        public void Dispose()
        {
            lock (_lock)
            {
                refs--;

                if (refs <= 0 && _RemoteProviderServiceHost != null )
                {
                    if (_RemoteProviderServiceHost.State == CommunicationState.Faulted)
                    {
                        _RemoteProviderServiceHost.Abort();
                    }
                    else
                    {
                        _RemoteProviderServiceHost.Close();
                    }

                    _RemoteProviderServiceHost = null;
                }
            }
        }

        #endregion
    }
}
